/*	$Id: util.c 6 2008-07-29 02:30:10Z phrakt $	*/
/*
 * Copyright (c) 2003 Jean-Francois Brousseau <jfb@fugusec.net>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/stat.h>

#include <err.h>
#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>

#include "pfsh.h"
#include "util.h"
#include "pforge.h"


#if defined(__linux__)
/* Linux only uses the first 6 X's */
#define PFORGE_TMPFILE_TEMPLATE  "/tmp/pforge.XXXXXX"
#else
#define PFORGE_TMPFILE_TEMPLATE "/tmp/pforge.XXXXXXXXXX"
#endif

#define PFORGE_TMPFILE_MAXATTEMPTS 10



/* binary <-> human-readable translations */

static const char pforge_bintohex[] = "0123456789abcdef";


/*
* pforge_binfmt()
*
* Format the first <sl> bytes of binary data from <s> into a human-readable
* representation and store the result in <d>, which is of size <dl>.  The
* result is guaranteed to be NUL-terminated.  The output format is determined
* by the base <b> and the delimiter character <delim>.  The base is expected to
* be one of the defined bases (binary, octal, decimal or hexadecimal).
* Returns the number of bytes that were successfully formatted from <s>, or -1
* on error.
*
* Truncation can be detected by comparing the result with <sl>.
*/

ssize_t
pforge_binfmt(int base, int flags, char delim, const void *src, size_t slen,
    char *dst, size_t dlen)
{
	int i, j, pad, nz;
	const uint8_t *sp, *spend;
	char *dp, *dpend;

	if (!isprint(delim)) {
		/* doesn't start very well */
		return (-1);
	}

	pad = (flags & PFUTIL_BFF_PAD) ? 1 : 0;
	nz = (flags & PFUTIL_BFF_NZ) ? 1 : 0;

	sp = (const uint8_t *)src;
	spend = src + slen;
	dp = dst;
	dpend = dst + dlen;  /* points to last byte */

	*dp = '\0';

	switch (base) {
	case PFUTIL_BF_BIN:
		break;
	case PFUTIL_BF_OCT:
		break;
	case PFUTIL_BF_DEC:
		while (sp < spend) {
			*(dp++) = delim;
		}
		break;
	case PFUTIL_BF_HEX:
		while (sp < spend) {
			if (!nz || (nz && (*sp != 0))) {
			i = *sp >> 4;
			if (pad || (i != 0)) {
				*(dp++) = pforge_bintohex[i];
				if (dp == dpend - 1)
					break;
			}

			j = *sp & 0xf;
			if (pad || (i != 0) || (j != 0)) {
				*(dp++) = pforge_bintohex[j];
				if (dp == dpend - 1)
					break;
			}
			}

			if (sp < spend - 1) {
				*(dp++) = delim;
				if (dp == dpend - 1)
					break;
			}

			sp++;
		}
		break;
	default:
		return (-1);
	}

	*dp = '\0';

	return (ssize_t)((size_t)src + slen - (size_t)sp);
}
